//
//  ToApplyViewController.m
//  tuanGG
//
//  Created by 微他 on 15/10/21.
//  Copyright (c) 2015年 weita. All rights reserved.
//

#import "ToApplyViewController.h"
#import "Header.h"
#import "WithdrawalTableViewCell.h"
@interface ToApplyViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
@property (nonatomic,strong)NSMutableArray *array;
@property (nonatomic,strong)NSArray *imageArray;
@property (nonatomic,strong)UITextField *textfield;
@property (nonatomic,strong)NSString *idString;
@property (nonatomic,strong)NSDictionary *dictionary;
@property (nonatomic)BOOL selected;
@end

@implementation ToApplyViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self addnavigationTitle:@"提现申请" navColor:@"4fd2be"];
    [self initlizeDataSource];
    [self initView];
}
- (void)initlizeDataSource {
    _selected = NO;
    NSLog(@"卡信息%@",_codeArray);
    [_array setArray:@[@[@"提现金额"],@[@"请选择银行"]]];
//    _array objectAtInde
    _imageArray = @[@"中国建设银行",@"中国工商银行"];
}
- (void)initView {

    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,ScreenWidth, ScreenHeight-64) style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.bounces= NO;
    [tableView registerClass:[WithdrawalTableViewCell class] forCellReuseIdentifier:@"CELL"];
    [self.view addSubview:tableView];

    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 100)];
    tableView.tableFooterView = line;
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(10, 40, ScreenWidth-20, 40)];
    [button setTitle:@"确定" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.layer.cornerRadius =5;
    [button addTarget:self action:@selector(sourceButton) forControlEvents:UIControlEventTouchUpInside];
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    button.backgroundColor = [UIColor colorWithHexString:@"4fd2be"];
    [line addSubview:button];

    _textfield = [[UITextField alloc]initWithFrame:CGRectMake(90, 0, ScreenWidth-100, 50)];
    _textfield.placeholder = @"请输入提现金额";
    _textfield.delegate = self;
    _textfield.clearButtonMode = UITextFieldViewModeWhileEditing;
}
#pragma mark - 事件实现
- (void)sourceButton {
    if ([_idString integerValue] < 1000) {
        [Common addAlert:@"请选择银行卡"];
    }else if (_textfield.text.length <= 0){
        [Common addAlert:@"请输入提现金额"];
    }else
        [self NetWorking];
}
#pragma mark - <UIActionSheetDelegate>


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    //    NSLog(@"%d",textField.tag);
    //    NSLog(@"%d\n,\n%d,\n%@,\n%@",range.location,range.length,string,textField.text);
    if (string.length == 0) {
        return YES;
    }
    if (string.length != 0) {
        //控制长度
        if(range.location>=20)
            return NO;

        const char * ch=[string cStringUsingEncoding:NSUTF8StringEncoding];
        if(*ch == 0)
            return YES;
        //字符0－9 和 .
        if( *ch != 46 && ( *ch<48 || *ch>57) )
            return NO;

        //有了小数点
        if([textField.text rangeOfString:@"."].length==1)
        {
            NSUInteger length=[textField.text rangeOfString:@"."].location;

            //小数点后面两位小数 且不能再是小数点
            if([[textField.text substringFromIndex:length] length]>2 || *ch ==46)   //3表示后面小数位的个数。。

                return NO;
        }

        return YES;

    }else {
        if (string.length >10) {
            [self addAlert:@"超出输入限制" otherButtonTitles:nil];
            return NO;
        }
    }
    
    return  YES;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 1) {
        return _codeArray.count+1;
    }else
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 || (indexPath.section == 1 && indexPath.row == 0)) {
        return 50;
    }else
    return 75;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WithdrawalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
    cell.textLabel.text = _array[indexPath.section][indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0) {
        [cell addSubview:_textfield];
    }
    if (indexPath.section == 1 && !indexPath.row == 0) {
        cell.selectedImage.hidden = NO;
        [cell.selectedImage sd_setImageWithURL:nil placeholderImage:IMGNAME(@"未选中状态")];
        NSString * string= nil;
        NSString *name = nil;
        string = [_codeArray[indexPath.row-1][@"bankcount"] substringFromIndex:15];
        name = [NSString stringWithFormat:@"%@",_codeArray[indexPath.row-1][@"bankname"]];
        NSLog(@"%@",name);
        [cell.typeImage sd_setImageWithURL:nil placeholderImage:IMGNAME(name)];
        cell.type.text = name;
        cell.codeDetails.text = [NSString stringWithFormat:@"尾号:%@  储蓄卡",string];
    }else if(indexPath.section == 1 && indexPath.row == 0){
        cell.selectedImage.hidden = YES;
        cell.textLabel.text = @"选择银行卡";
    }else if (indexPath.section == 0 ||(indexPath.section == 1 && indexPath.row == 0)){
        cell.selectedImage.hidden = YES;

    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView endEditing:YES];
    NSLog(@"%ld",(long)indexPath.row);
    if (indexPath.row != 0 ) {
       _idString = _codeArray[indexPath.row-1][@"id"];
    }
    NSLog(@"%@",_idString);
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    _textfield.keyboardType = UIKeyboardTypeNamePhonePad;
}
#pragma mark - 网络请求
- (void)NetWorking {
    _dictionary = @{@"price":_textfield.text,@"bankcardid":_idString};
    [GLAFRequest startRequest:cashask_url paramsDic:_dictionary defaultdialog:YES ispost:YES handleSuccess:^(id sta, NSDictionary *paramsDic) {
        NSLog(@"%@",sta);
        if ([sta[@"code"]integerValue] == 1) {
            [Common addAlert:@"提现成功,注意查看"];
            [self.navigationController popViewControllerAnimated:YES];
        }
    } doFinally:^(NSDictionary *paramsDic) {

    }];
}
@end
